package com.itcast.demo_one

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.itcast.demo_one.adapter.ColorAdapter

class ViewPagerTwoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_view_pager_two)

        // 1. 获取ViewPager2
        val viewPager = findViewById<ViewPager2>(R.id.viewPager)
        val adapter = ColorAdapter()
        viewPager.adapter = adapter

        // 2. 设置滚动的方向
        viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL

        // 3. 监听页面滚动
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                Toast.makeText(this@ViewPagerTwoActivity, "当前页面：$position", Toast.LENGTH_SHORT).show()
            }
        })
    }
}